<template>
  <div>
    <!-- 导航 -->
    <Nav>
      <el-breadcrumb-item
        slot="nav"
        v-for="(item,index) in breadcrumbList"
        :key="index"
        :to="{ path : item.href}"
      >{{item.name}}</el-breadcrumb-item>
    </Nav>
    <el-card>
      <el-button type="primary" @click="addClassification">添加分类</el-button>
      <!-- 表单内容 -->
      <tree-table
        :data="categoriesList"
        :columns="columns"
        show-index
        border
        :expand-type="false"
        :selection-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="info" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operation">
          <el-button type="primary" icon="el-icon-edilt" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>-->
      <Pagination
        :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange"
        :querInfo="queryInfo"
        :total="total"
      ></Pagination>
    </el-card>

    <!-- 添加分类 对话框 -->

    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%" @close="closeAddCate">
      <el-form
        :model="addCategoriesForm"
        :rules="addCategoriesRules"
        ref="addCategoriesRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCategoriesForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- options 用来指定数据源 -->
          <!-- props 用来指定配置对象 -->
          <el-cascader
            expand-trigger="hover"
            :options="parentCateList"
            :props="cascaderProps"
            v-model="selectedKeys"
            @change="parentCateChanged"
            clearable
            change-on-select
            class="select"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCatList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Nav from '../nav'
import Pagination from '../pagination'
export default {
  data() {
    return {
      breadcrumbList: [
        {
          name: '首页',
          href: '/welcome',
        },
        {
          name: '商品管理',
        },
        {
          name: '商品分类',
        },
      ],
      // 商品分类列表数据
      categoriesList: [],
      // 分页信息
      queryInfo: {
        type: 3,
        //   当前页码
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 5,
      },
      //   每一列
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          // 渲染模板插槽
          type: 'template',
          template: 'isOk',
        },
        {
          label: '排序',
          // 渲染模板插槽
          type: 'template',
          template: 'order',
        },
        {
          label: '排序',
          // 渲染模板插槽
          type: 'template',
          template: 'operation',
        },
      ],
      // 添加分类 弹出对话框
      addDialogVisible: false,
      // 添加分类表单数据对象
      addCategoriesForm: {
        cat_name: '',
        //父级分类的等级
        cat_pid: 0,
        // 当前自己分类的等级
        cat_level: 0,
      },
      // 添加分类的验证规则
      addCategoriesRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      // 父级数据列表
      parentCateList: [],
      // 指定级联选择器的配置对象
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
      },
      // 选中的父级id
      selectedKeys: [],
      total: 0,
    }
  },
  methods: {
    //   获取商品数据
    async getcategoriesList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取用户列表失败')
      this.categoriesList = res.data.result
      this.total = res.data.total
    },
    // 监听 pagesize 改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getcategoriesList()
    },
    // 监听 pagenum 改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getcategoriesList()
    },
    // 点击按钮添加分类 弹出对话框
    addClassification() {
      this.getParentList()
      this.addDialogVisible = true
    },
    // 获取父级分类列表
    async getParentList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败！')
      }
      this.parentCateList = res.data
    },

    // 级联选择框发生改变
    parentCateChanged() {
      // 当级联发生改变的时候，就改变级联选择器中的等级变化
      if (this.selectedKeys.length > 0) {
        this.addCategoriesForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ]
        this.addCategoriesForm.cat_level = this.selectedKeys.length
      } else {
        // 如果没选择 父级的id就是0
        this.addCategoriesForm.cat_pid = 0
        // 如果没选择当前id就是0
        this.addCategoriesForm.cat_level = 0
      }
    },
    // 点击确定添加分类，确定添加到列表
    addCatList() {
      this.$refs.addCategoriesRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          'categories',
          this.addCategoriesForm
        )
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getcategoriesList()
        this.addDialogVisible = false
      })
    },

    // 关闭添加分类对话框事件
    closeAddCate() {
      this.$refs.addCategoriesRef.resetFields()
      this.selectedKeys = []
      this.selectedKeys.cat_pid = 0
      this.selectedKeys.cat_level = 0
    },
  },
  created() {
    this.getcategoriesList()
  },
  components: {
    Nav,
    Pagination
  },
}
</script>

<style>
.select {
  width: 100%;
}
</style>